import common from '../vendor/common'
import '../scss/user_house_resources.scss'
import { getRegion, launch } from '../vendor/api'
try{
    const mui = common.mui
    var setData = null
    var data = {}
    var area_id = 1
    var appellation = null //称谓
    var tel = null //电话号码
    var acreage = null //面积
    var building_name = null //楼盘名称
    mui.init({
        swipeBack:false //启用右滑关闭功能
    });
    mui.plusReady(function(){
        window.addEventListener('region', function(e){
            // 获取区域信息
            getRegion().then((res) => {
                setData = res.data
            })
            // 初始化页面所有数据
            data = {}
            area_id = 1
            appellation = null
            tel = null
            acreage = null
            building_name = null
            mui('#appellation')[0].value = null
            mui('#tel')[0].value = null
            mui('#buildingName')[0].value = null
            mui('#acreage')[0].value = null
        })
        mui('#down')[0].addEventListener('tap',function(){
            mui('#appellation')[0].blur()
            mui('#tel')[0].blur()
            //获取弹出列表组建，假如是二联则在括号里面加入{layer:2}
            var picker = new mui.PopPicker()
            //设置弹出列表的信息
            picker.setData(setData)
            picker.show(function(SelectedItem) {
                area_id = SelectedItem[0].value
                mui('.title')[0].innerHTML = SelectedItem[0].text
            })
        })
        // 提交
        mui('.loginOut').on('tap', 'button', function(){
            // 获取输入框里的值
            appellation = mui('#appellation')[0].value
            tel = mui('#tel')[0].value
            acreage = mui('#acreage')[0].value
            building_name = mui('#buildingName')[0].value
            data.name = appellation
            data.tel = tel
            data.acreage = acreage
            data.building_name = building_name
            data.area_guid = area_id
            data.area_name = document.querySelectorAll('div.title.mui-col-xs-4')[0].innerHTML
            data.page_source = '个人中心-投放房源'
            data.source = 5
            data.demand = 1
            // 隐藏键盘
            mui('#appellation')[0].blur()
            mui('#tel')[0].blur()
            mui('#acreage')[0].blur()
            mui('#buildingName')[0].blur()
            // 验证提交
            if(!appellation || appellation.trim() == ''){
                mui.alert('请输入称谓')
            } else if(!tel || tel.trim() == ''){
                mui.alert('请输入电话')
            } else if(!area_id){
                mui.alert('请选择区域')
            } else if(!acreage || acreage.trim() == ''){
                mui.alert('请输入面积')
            } else if(!building_name || building_name.trim() == ''){
                mui.alert('请输入楼盘名称')
            } else {
                launch(data).then((res) => {
                    if(res.data){
                        mui.toast('信息提交成功，楚楼网30分钟内联系您')
                        // 获取当前页面webview
                        var ws=plus.webview.currentWebview();
                        plus.webview.hide(ws);
                    }
                })
            }
        })
    })
}catch(e){
    console.log('user_house_resources.html出错了')
}